import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { getAllRolesListNoByTenant, getAllTenantList,getDeptList } from './user.api';
import { rules } from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
export const columns: BasicColumn[] = [
  // {
  //   title: '用户账号',
  //   dataIndex: 'username',
  //   width: 120,
  // },
  {
    title: '头像',
    dataIndex: 'avatar',
    width: 120,
    customRender: render.renderAvatar,
  },
  {
    title: '姓名',
    dataIndex: 'realname',
    width: 100,
  },
  
  // {
  //   title: '性别',
  //   dataIndex: 'sex',
  //   width: 80,
  //   sorter: true,
  //   customRender: ({ text }) => {
  //     return render.renderDict(text, 'sex');
  //   },
  // },
  // {
  //   title: '生日',
  //   dataIndex: 'birthday',
  //   width: 100,
  // },
  {
    title: '联系电话',
    dataIndex: 'phone',
    width: 100,
  },
  {
    title: '所属部门',
    width: 150,
    dataIndex: 'orgCodeTxt',
  },
  {
    title: '角色',
    width: 150,
    dataIndex: 'roleName',
  },
  {
    title: '可独立登录席位',
    width: 150,
    dataIndex: 'directlySeatIdList_dictText',
  },
  {
    title: '见习席位',
    width: 150,
    dataIndex: 'studySeatIdList_dictText',
  },
  {
    title: '教员/学员',
    width: 150,
    dataIndex: 'userType_dictText',
  },
  {
    title: '状态',
    dataIndex: 'status_dictText',
    width: 80,
  },
];

export const recycleColumns: BasicColumn[] = [
  {
    title: '用户账号',
    dataIndex: 'username',
    width: 100,
  },
  {
    title: '用户姓名',
    dataIndex: 'realname',
    width: 100,
  },
  {
    title: '头像',
    dataIndex: 'avatar',
    width: 80,
    customRender: render.renderAvatar,
  },
  {
    title: '性别',
    dataIndex: 'sex',
    width: 80,
    sorter: true,
    customRender: ({ text }) => {
      return render.renderDict(text, 'sex');
    },
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    label: '姓名',
    field: 'realname',
    component: 'JInput',
   //colProps: { span: 6 },
  },
  {
    label: '联系电话',
    field: 'phone',
    component: 'Input',
    //colProps: { span: 6 },
  },
  {
    label: '部门',
    field: 'orgCode',
    component: 'JDictSelectTag',
    componentProps: ({ formModel, formActionType }) => {
      return {
        dictCode: 'sys_depart,depart_name,org_code,org_code like "A01A%"',
      }
    },
  },
  {
    label: '角色',
    field: 'roleCode',
    component: 'JDictSelectTag',
    componentProps: ({ formModel, formActionType }) => {
      return {
        dictCode: 'sys_role,role_name,role_code,role_code!="admin"',
      }
    },
  },
  // {
  //   label: '用户状态',
  //   field: 'status',
  //   component: 'JDictSelectTag',
  //   componentProps: {
  //     dictCode: 'user_status',
  //     placeholder: '请选择状态',
  //     stringToNumber: true,
  //   },
  //  //colProps: { span: 6 },
  // },
];

export const formSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    label: '头像',
    field: 'avatar',
    component: 'JImageUpload',
    componentProps: {
      fileMax: 1,
    },
  },
  {
    label: '用户账号',
    field: 'username',
    component: 'Input',
    required: true,
    dynamicDisabled: ({ values }) => {
      return !!values.id;
    },
    dynamicRules: ({ model, schema }) => rules.duplicateCheckRule('sys_user', 'username', model, schema, true),
  },
  {
    label: '登录密码',
    field: 'password',
    component: 'StrengthMeter',
    componentProps:{
      autocomplete: 'new-password',
    },
    rules: [
      {
        required: true,
        message: '请输入登录密码',
      },
      {
        pattern: /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+`\-={}:";'<>?,./]).{8,}$/,
        message: '密码由8位数字、大小写字母和特殊符号组成!',
      },
    ],
  },
  {
    label: '确认密码',
    field: 'confirmPassword',
    component: 'InputPassword',
    dynamicRules: ({ values }) => rules.confirmPassword(values, true),
  },
  {
    label: '用户姓名',
    field: 'realname',
    required: true,
    component: 'Input',
  },
  {
    label: '性别',
    field: 'sex',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'sex',
      placeholder: '请选择性别',
      stringToNumber: true,
    },
  },
  {
    label: '联系电话',
    field: 'phone',
    component: 'Input',
    required: true,
    dynamicRules: ({ model, schema }) => {
      return [
        { ...rules.duplicateCheckRule('sys_user', 'phone', model, schema, true)[0], trigger: 'blur' },
        { pattern: /^1[3456789]\d{9}$/, message: '手机号码格式有误', trigger: 'blur' },
      ];
    },
  },
  {
    label: '工号',
    field: 'workNo',
    required: true,
    component: 'Input',
    dynamicRules: ({ model, schema }) => rules.duplicateCheckRule('sys_user', 'work_no', model, schema, true),
  },
  // {
  //   label: '职务',
  //   field: 'post',
  //   required: false,
  //   component: 'JSelectPosition',
  //   componentProps: {
  //     labelKey: 'name',
  //   },
  // },
  {
    label: '所属部门',
    field: 'selecteddeparts',
    component: 'JDictSelectTag',
    required: true,
    componentProps: ({ formActionType, formModel }) => {
      return {
          dictCode: 'sys_depart,depart_name,id,org_code like "A01A%"',
        sync: false,
        checkStrictly: true,
        defaultExpandLevel: 2,
        multiple:false,
        // params:{"orgCode":"A01"},
        // parentKey:'1862836336841105409',
        onChange: (value) => {
          console.log("选择部门==",value)
          const { updateSchema } = formActionType;
          
          if(value ){
            getDeptList({id:value}).then(res=>{
              console.log("查询部门",res);
              if(res && res.length > 0){
                formModel.orgCode = res[0].orgCode
                updateSchema([
                  {
                    field: 'directlySeatIdList',
                    show:true
                  },
                ]);
              }
            })
          }else{
            updateSchema([
              {
                field: 'directlySeatIdList',
                show:false
              },
              {
                field: 'studySeatIdList',
                show:false
              },
            ]);
          }
          
           
         
          //update-begin---author:wangshuai---date:2024-05-11---for:【issues/1222】用户编辑界面“所属部门”与“负责部门”联动出错整---
          if(!value){
            formModel.departIds = [];
            return;
          }
          //update-end---author:wangshuai---date:2024-05-11---for:【issues/1222】用户编辑界面“所属部门”与“负责部门”联动出错整---
          //所属部门修改后更新负责部门数据
          formModel.departIds && (formModel.departIds = formModel.departIds.filter((item) => values.value.indexOf(item) > -1));
        },
      };
    },
  },
  // {
  //   label: '所属部门',
  //   field: 'selecteddeparts',
  //   component: 'JSelectDept',
  //   required: true,
  //   componentProps: ({ formActionType, formModel }) => {
  //     return {
  //       sync: false,
  //       checkStrictly: true,
  //       defaultExpandLevel: 2,
  //       multiple:false,
  //       // params:{"orgCode":"A01"},
  //       // parentKey:'1862836336841105409',
  //       onSelect: (options, values) => {
  //         console.log("选择部门==",values)
  //         const { updateSchema } = formActionType;
  //         //所属部门修改后更新负责部门下拉框数据
  //         updateSchema([
  //           {
  //             field: 'departIds',
  //             componentProps: { options },
  //           },
  //         ]);
  //         if(values && values?.value.length > 0){
  //           getDeptList({id:values?.value[0]}).then(res=>{
  //             console.log("查询部门",res);
  //             if(res && res.length > 0){
  //               formModel.orgCode = res[0].orgCode
  //               updateSchema([
  //                 {
  //                   field: 'directlySeatIdList',
  //                   show:true
  //                 },
  //               ]);
  //             }
  //           })
  //         }else{
  //           updateSchema([
  //             {
  //               field: 'directlySeatIdList',
  //               show:false
  //             },
  //             {
  //               field: 'studySeatIdList',
  //               show:false
  //             },
  //           ]);
  //         }
          
           
         
  //         //update-begin---author:wangshuai---date:2024-05-11---for:【issues/1222】用户编辑界面“所属部门”与“负责部门”联动出错整---
  //         if(!values){
  //           formModel.departIds = [];
  //           return;
  //         }
  //         //update-end---author:wangshuai---date:2024-05-11---for:【issues/1222】用户编辑界面“所属部门”与“负责部门”联动出错整---
  //         //所属部门修改后更新负责部门数据
  //         formModel.departIds && (formModel.departIds = formModel.departIds.filter((item) => values.value.indexOf(item) > -1));
  //       },
  //     };
  //   },
  // },
  {
    label: '角色',
    field: 'selectedroles',
    component: 'ApiSelect',
    required: true,
    componentProps: {
      mode: 'single',
      api: getAllRolesListNoByTenant,
      labelField: 'roleName',
      valueField: 'id',
      immediate: false,
    },
  },
  {
    label: '可独立登录席位',
    field: 'directlySeatIdList',
    component: 'JCheckbox',
    show:false,
    componentProps: ({ formModel, formActionType }) => {
      return {
        dictCode: `gbiac_seat,name,id,org_code = "${formModel.orgCode}"`,
      }
    },
  },
  {
    label: '教员或学员',
    field: 'userType',
    component: 'JDictSelectTag',
    required: true,
    componentProps: ({ formActionType, formModel }) => {
      return {
        dictCode: 'user_types',
        
        onSelect: (options, values) => {
          const { updateSchema } = formActionType;
          console.log("values===",values);
          
          if(values.value == 'teacher'){
            updateSchema([
              {
                field: 'studySeatIdList',
                show:false
              },
            ]);
          }else{
            updateSchema([
              {
                field: 'studySeatIdList',
                show:true
              },
            ]);
          }
          
        },
      }
    },
  },
  {
    label: '见习席位选择',
    field: 'studySeatIdList',
    component: 'JCheckbox',
    show:false,
    componentProps: ({ formModel, formActionType }) => {
      return {
        dictCode: `gbiac_seat,name,id,org_code = "${formModel.orgCode}"`,
      }
    },
  },
  // {
  //   label: '身份',
  //   field: 'userIdentity',
  //   component: 'RadioGroup',
  //   defaultValue: 1,
  //   componentProps: ({ formModel }) => {
  //     return {
  //       options: [
  //         { label: '普通用户', value: 1, key: '1' },
  //         { label: '上级', value: 2, key: '2' },
  //       ],
  //       onChange: () => {
  //         formModel.userIdentity == 1 && (formModel.departIds = []);
  //       },
  //     };
  //   },
  // },
  // {
  //   label: '负责部门',
  //   field: 'departIds',
  //   component: 'Select',
  //   componentProps: {
  //     mode: 'multiple',
  //   },
  //   ifShow: ({ values }) => values.userIdentity == 2,
  // },
  
  // {
  //   label: '生日',
  //   field: 'birthday',
  //   component: 'DatePicker',
  // },
 
  // {
  //   label: '邮箱',
  //   field: 'email',
  //   component: 'Input',
  //   required: true,
  //   dynamicRules: ({ model, schema }) => {
  //     return [
  //       { ...rules.duplicateCheckRule('sys_user', 'email', model, schema, true)[0], trigger: 'blur' },
  //       { ...rules.rule('email', false)[0], trigger: 'blur' },
  //     ];
  //   },
  // },
 
  // {
  //   label: '座机',
  //   field: 'telephone',
  //   component: 'Input',
  //   rules: [{ pattern: /^0\d{2,3}-[1-9]\d{6,7}$/, message: '请输入正确的座机号码' }],
  // },
  // {
  //   label: '工作流引擎',
  //   field: 'activitiSync',
  //   defaultValue: 1,
  //   component: 'JDictSelectTag',
  //   componentProps: {
  //     dictCode: 'activiti_sync',
  //     type: 'radio',
  //     stringToNumber: true,
  //   },
  // },
];

export const formPasswordSchema: FormSchema[] = [
  {
    label: '用户账号',
    field: 'username',
    component: 'Input',
    componentProps: { readOnly: true },
  },
  {
    label: '登录密码',
    field: 'password',
    component: 'StrengthMeter',
    componentProps: {
      placeholder: '请输入登录密码',
    },
    rules: [
      {
        required: true,
        message: '请输入登录密码',
      },
      {
        pattern: /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+`\-={}:";'<>?,./]).{8,}$/,
        message: '密码由8位数字、大小写字母和特殊符号组成!',
      },
    ],
  },
  {
    label: '确认密码',
    field: 'confirmPassword',
    component: 'InputPassword',
    dynamicRules: ({ values }) => rules.confirmPassword(values, true),
  },
];

export const formAgentSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    field: 'userName',
    label: '用户名',
    component: 'Input',
    componentProps: {
      readOnly: true,
      allowClear: false,
    },
  },
  {
    field: 'agentUserName',
    label: '代理人用户名',
    required: true,
    component: 'JSelectUser',
    componentProps: {
      rowKey: 'username',
      labelKey: 'realname',
      maxSelectCount: 10,
    },
  },
  {
    field: 'startTime',
    label: '代理开始时间',
    component: 'DatePicker',
    required: true,
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      placeholder: '请选择代理开始时间',
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'endTime',
    label: '代理结束时间',
    component: 'DatePicker',
    required: true,
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      placeholder: '请选择代理结束时间',
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'status',
    label: '状态',
    component: 'JDictSelectTag',
    defaultValue: '1',
    componentProps: {
      dictCode: 'valid_status',
      type: 'radioButton',
    },
  },
];

export const formQuitAgentSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    field: 'userName',
    label: '用户名',
    component: 'Input',
    componentProps: {
      readOnly: true,
      allowClear: false,
    },
  },
  {
    field: 'agentUserName',
    label: '交接人员',
    //required: true,
    component: 'JSelectUser',
    componentProps: {
      rowKey: 'username',
      labelKey: 'realname',
      maxSelectCount: 1,
    },
  },
  {
    field: 'startTime',
    label: '交接开始时间',
    component: 'DatePicker',
    //required: true,
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      placeholder: '请选择交接开始时间',
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'endTime',
    label: '交接结束时间',
    component: 'DatePicker',
    //required: true,
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      placeholder: '请选择交接结束时间',
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'status',
    label: '状态',
    component: 'JDictSelectTag',
    defaultValue: '1',
    componentProps: {
      dictCode: 'valid_status',
      type: 'radioButton',
    },
  },
];

//租户用户列表
export const userTenantColumns: BasicColumn[] = [
  {
    title: '用户账号',
    dataIndex: 'username',
    width: 120,
  },
  {
    title: '用户姓名',
    dataIndex: 'realname',
    width: 100,
  },
  {
    title: '头像',
    dataIndex: 'avatar',
    width: 120,
    customRender: render.renderAvatar,
  },
  {
    title: '手机号',
    dataIndex: 'phone',
    width: 100,
  },
  {
    title: '所属部门',
    width: 150,
    dataIndex: 'orgCodeTxt',
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 80,
    customRender: ({ text }) => {
      if (text === '1') {
        return '正常';
      } else if (text === '3') {
        return '审批中';
      } else {
        return '已拒绝';
      }
    },
  },
];

//用户租户搜索表单
export const userTenantFormSchema: FormSchema[] = [
  {
    label: '账号',
    field: 'username',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '名字',
    field: 'realname',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '性别',
    field: 'sex',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'sex',
      placeholder: '请选择性别',
      stringToNumber: true,
    },
    colProps: { span: 6 },
  },
];
